package br.org.demolay.webservice;

import java.util.ArrayList;
import java.util.List;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

;

public final class Service {
	public long Logar(int cid, String senha) {
		SoapObject soap = new SoapObject("http://tempuri.org/", "Logar");
		soap.addProperty("cid", cid);
		soap.addProperty("senha", senha);
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(soap);

		String url = "http://radardeofertas.no-ip.org:51/DemolayWebService.asmx";
		HttpTransportSE httpTransport = new HttpTransportSE(url);

		try {

			httpTransport.call("http://tempuri.org/Logar", envelope);

			Object msg = envelope.getResponse();

			return Long.parseLong(msg.toString());
		} catch (Exception ex) {
			ex.printStackTrace();
			return 0;
		}
	}

	public String Nome(long idUsuario) {
		SoapObject soap = new SoapObject("http://tempuri.org/", "Nome");
		soap.addProperty("idUsuario", idUsuario);
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(soap);

		String url = "http://radardeofertas.no-ip.org:51/DemolayWebService.asmx";
		HttpTransportSE httpTransport = new HttpTransportSE(url);

		try {

			httpTransport.call("http://tempuri.org/Nome", envelope);

			return envelope.getResponse().toString();
		} catch (Exception ex) {
			ex.printStackTrace();
			return "";
		}
	}

	public List<Contato> Contatos(long idUsuario) {
		SoapObject soap = new SoapObject("http://tempuri.org/", "Contatos");
		soap.addProperty("idUsuario", idUsuario);
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(soap);

		String url = "http://radardeofertas.no-ip.org:51/DemolayWebService.asmx";
		HttpTransportSE httpTransport = new HttpTransportSE(url);

		try {

			httpTransport.call("http://tempuri.org/Contatos", envelope);

			SoapObject obj = (SoapObject) envelope.getResponse();
			List<Contato> listResult = new ArrayList<Contato>();
			for (int i = 0; i < obj.getPropertyCount(); i++) {
				Object property = obj.getProperty(i);
				if (property instanceof SoapObject) {
					SoapObject contato = (SoapObject) property;
					Contato con = new Contato(contato.getProperty("Nome")
							.toString(), contato.getProperty("Telefone")
							.toString(), contato.getProperty("Nivel")
							.toString(), contato.getProperty("Email")
							.toString(),contato.getProperty("Celular")
							.toString());
					listResult.add(con);
				}
			}

			return listResult;
		} catch (Exception ex) {
			ex.printStackTrace();
			return null;
		}
	}

	public List<Mensalidade> Mensalidades(long idUsuario){
		SoapObject soap = new SoapObject("http://tempuri.org/", "Mensalidades");
		soap.addProperty("idUsuario", idUsuario);
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(soap);

		String url = "http://radardeofertas.no-ip.org:51/DemolayWebService.asmx";
		HttpTransportSE httpTransport = new HttpTransportSE(url);

		try {

			httpTransport.call("http://tempuri.org/Mensalidades", envelope);

			SoapObject obj = (SoapObject) envelope.getResponse();
			List<Mensalidade> listResult = new ArrayList<Mensalidade>();
			for (int i = 0; i < obj.getPropertyCount(); i++) {
				Object property = obj.getProperty(i);
				if (property instanceof SoapObject) {
					SoapObject mensalidade = (SoapObject) property;
					Mensalidade men = new Mensalidade(mensalidade.getProperty("Data")
							.toString(), mensalidade.getProperty("DataPagamento")
							.toString());
					listResult.add(men);
				}
			}

			return listResult;
		} catch (Exception ex) {
			ex.printStackTrace();
			return null;
		}
	}
}

